//
//  STTabViewCell.swift
//  iPhone
//
//  Created by Toj on 11/20/23.
//

import UIKit

class STTabViewCell: UICollectionViewCell {
    
    var cellStyle: STPageTitleCellStyle? { didSet {
        titleLabel.font = cellStyle?.textFont
    } }
    
    var tab: STTabViewModel? { didSet {
        guard let tab = tab else { return }
        
        titleLabel.text = tab.title
        
        let titleColor = tab.isSelected ? "#0087EB".to_color : "#666666".to_color
        titleLabel.textColor = titleColor
        
        let iconURL = URL(string: tab.icon)
        iconView.kf.setImage(with: iconURL)
        
        tipLabel.text = tab.tip
    } }
    
    var isSelectedTitle = false { willSet {
        backgroundColor = newValue ? cellStyle?.textSelectedBackgroundColor : cellStyle?.textBackgroundColor
        titleLabel.textColor = newValue ? cellStyle?.textSelectedColor : cellStyle?.textColor
    } }
    
    private(set) lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.font = 16.to_rbtMediumFont
        return view
    }()

    /** <#NOTE#> **/
    private lazy var iconView: UIImageView = {
        let view = UIImageView()
        view.contentMode = .scaleAspectFit
        return view
    }()
    
    /** <#NOTE#> **/
    private lazy var tipLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#FF1921".to_color
        view.font = 12.to_rbtRegularFont
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupMakeAddSubviews()
        setupMakeInitialize()
        setupMakeLayoutSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - 初始化设置
private extension STTabViewCell {
      
    func setupMakeAddSubviews() {
        contentView.addSubview(titleLabel)
        contentView.addSubview(iconView)
        contentView.addSubview(tipLabel)
    }
    
    func setupMakeLayoutSubviews() {
        titleLabel.snp.remakeConstraints { make in
            make.centerY.left.equalTo(contentView)
        }
        
        tipLabel.snp.remakeConstraints { make in
            make.left.equalTo(titleLabel.snp.right)
            make.centerY.equalTo(titleLabel.snp.top)
        }
        
        iconView.snp.remakeConstraints { make in
            make.height.centerY.right.equalTo(contentView)
            make.width.equalTo(20)
        }
    }
    
    func setupMakeInitialize() {
        backgroundColor = .clear
        contentView.backgroundColor = .clear
    }
}
